var lunarInfo = new Array(
0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,
0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,
0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,
0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,
0x06ca0,0x0b550,0x15355,0x04da0,0x0a5d0,0x14573,0x052d0,0x0a9a8,0x0e950,0x06aa0,
0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,
0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b5a0,0x195a6,
0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,
0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,
0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,
0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,
0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,
0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,
0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0);

var solarMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var Gan = new Array("甲","乙","丙","丁","戊","己","庚","辛","壬","癸");
var Zhi = new Array("子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥");
var Animals = new Array("鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪");
var solarTerm = new Array("小寒","大寒","立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种","夏至","小暑"," 大暑","立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至")
var sTermInfo = new Array(0,21208,42467,63836,85337,107014,128867,150921,173149,195551,218072,240693,263343,285989,308563,331033,353350,375494,397447,419210,440795,462224,483532,504758)
var nStr1 = new Array('日','一','二','三','四','五','六','七','八','九','十')
var nStr2 = new Array('初','十','廿','卅','　')
var monthName = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");

//国历节日 *表示放假日
var sFtv = new Array(
"0101*元旦",
"0214 情人节",
"0308 妇女节",
"0312 植树节",
"0315 消费者权益日",
"0401 愚人节",
"0501*劳动节",
"0504 青年节",
"0512 护士节",
"0601 儿童节",
"0701 建党节 香港回归纪念",
"0801 建军节",
"0909 毛泽东逝世纪念",
"0910 教师节",
"0928 孔子诞辰",
"1001*国庆节",
"1006 老人节",
"1024 联合国日",
"1112 孙中山诞辰纪念",
"1220 澳门回归纪念",
"1225 圣诞节",
"1226 毛泽东诞辰纪念")

//农历节日 *表示放假日
var lFtv = new Array(
"0101*春节",
"0102*初二",
"0115 元宵节",
"0505*端午节",
"0707 七夕情人节",
"0715 中元节",
"0815*中秋节",
"0909 重阳节",
"1208 腊八节",
"1223 小年",
"0100*除夕")

var wFtv = new Array(
"0520 母亲节", "0630 父亲节")

function lYearDays(y) {
	//月份的基数为29天，12个月就是348天
 var i, sum = 348
 //从最高位开始，依次查看每个月份是大月还是小月，大月就再加1天
 for(i=0x8000; i>0x8; i>>=1) sum += (lunarInfo[y-1900] & i)? 1: 0
 //再看看是不是有闰月，有闰月的话，就加上闰月的天数
 return(sum+leapDays(y))
}

function leapDays(y) {
	//是否存在闰月
 if(leapMonth(y)) 
 	//闰大月还是闰小月
 	return((lunarInfo[y-1900] & 0x10000)? 30: 29)
 else return(0)
}

function leapMonth(y) {
	//是否有闰月，闰几月
 return(lunarInfo[y-1900] & 0xf)
}

function monthDays(y,m) {
	//通过偏移m个位，来决定指定月份的天数。太妙了。
 return( (lunarInfo[y-1900] & (0x10000>>m))? 30: 29 )
}

function Lunar(objDate) {

 var i, leap=0, temp=0
 var baseDate = new Date(1900,0,31)
 var offset = (objDate - baseDate)/86400000

 this.dayCyl = offset + 40
 this.monCyl = 14

 for(i=1900; i<2050 && offset>0; i++) {
 temp = lYearDays(i)
 offset -= temp
 this.monCyl += 12
 }

 if(offset<0) {
 offset += temp;
 i--;
 this.monCyl -= 12
 }

 this.year = i
 this.yearCyl = i-1864

 leap = leapMonth(i)
 this.isLeap = false

 for(i=1; i<13 && offset>0; i++) {
 if(leap>0 && i==(leap+1) && this.isLeap==false)
 { --i; this.isLeap = true; temp = leapDays(this.year); }
 else
 { temp = monthDays(this.year, i); }

 if(this.isLeap==true && i==(leap+1)) this.isLeap = false

 offset -= temp
 if(this.isLeap == false) this.monCyl ++
 }

 if(offset==0 && leap>0 && i==leap+1)
 if(this.isLeap)
 { this.isLeap = false; }
 else
 { this.isLeap = true; --i; --this.monCyl;}

 if(offset<0){ offset += temp; --i; --this.monCyl; }

 this.month = i
 this.day = offset + 1
}

function solarDays(y,m) {
 if(m==1)
 return(((y%4 == 0) && (y%100 != 0) || (y%400 == 0))? 29: 28)
 else
 return(solarMonth[m])
}
function cyclical(num) {
 return(Gan[num%10]+Zhi[num%12])
}


function sTerm(y,n) {
 var offDate = new Date( ( 31556925974.7*(y-1900) + sTermInfo[n]*60000 ) + Date.UTC(1900,0,6,2,5) )
 return(offDate.getUTCDate())
}



var customMail = Class.create();
customMail.prototype = {
    initialize : function(){
                     this.day = new Date();
                     this.days = [];
             },
    weekOfYear: function (year,   month,   day){
		  var   date1   =   new   Date(year,   0,   1);
		  var   date2   =   new   Date(year,   month-1,   day,   1);
       
		  var   dayMS   =   24*60*60*1000;
		  var   firstDay   =   (7-date1.getDay())*dayMS;
		  var   weekMS   =   7*dayMS;
		  date1   =   date1.getTime();
		  date2   =   date2.getTime();
		  return   Math.ceil((date2-date1-firstDay)/weekMS)+1;
      },
    dateFromWeek : function (year,   week,   day){
		  var   date1   =   new   Date(year,   0,   1);
		  var   dayMS   =   24*60*60*1000;
		  var   firstDay   =   (7-date1.getDay() )*dayMS;
		  var   weekMS   =   (week-2)*7*dayMS;
		  var   result   =   date1.getTime()+firstDay+weekMS+day*dayMS;
		  date1.setTime(result);
		 return   date1;
      },
    dateAdd: function ( d , interval , number) {
                 if( d == null || d == undefined ){
                     return null;
                 }
		switch(interval) {
		case "y" :
			d.setFullYear(d.getFullYear()+number);
			return d;
		case "q" :
			d.setMonth(d.getMonth()+number*3);
			return d;
		case "m" :
			d.setMonth(d.getMonth()+number);
			return d;
		case "w" :
			d.setDate(d.getDate()+number*7);
			return d;
		case "d" :
			d.setDate(d.getDate()+number);
			return d;
		case "h" :
			d.setHours(d.getHours()+number);
			return d;
		case "m" :
			d.setMinutes(d.getMinutes()+number);
			return d;
		case "s" :
			d.setSeconds(d.getSeconds()+number);
			return d;
		default :
			d.setDate(d.getDate()+number);
			return d;
		}
	},
    monthDays: function (iYear,iMonth){
		iYear = parseInt(iYear);
		iMonth = parseInt(iMonth);
		var iDay = 31;
		if((iMonth==4||iMonth==6||iMonth==9||iMonth==11)&&iDay == 31){
			iDay=30;
			}
		if(iMonth==2 ){
			if( this.isLeapYear(iYear)){
				iDay = 29;
			}else {
				iDay = 28;
			}
		}
		return iDay;
	},
    isLeapYear: function (iYear) {
		if(iYear+"" == "undefined" || iYear+""== "null" || iYear+"" == "") {
			return false;
		}
		iYear = parseInt(iYear);
		if((iYear % 4 == 0 && iYear % 100 != 0) || iYear % 400 == 0){
			return true
		}else{
			return false
		};
	},
    isFesDays : function( day ){
                         var m = day.getMonth() + 1;
                         var d = day.getDate();
                    switch( m ){
                        case 1: {
                                    switch( d ){
                                        case 1 :{ return 1; }break;
                                    }
                                    return false;
                                };break;
                        case 2:{
                                return false;
                               };break;
                        case 3:{
                                return false;
                               };break;
                        case 4:{
                                return false;
                               };break;
                        case 5:{
                                return false;
                               };break;
                        case 6:{
                                return false;
                               };break;
                        case 7:{
                                return false;
                               };break;
                        case 8:{
                                return false;
                               };break;
                        case 9:{
                                return false;
                               };break;
                        case 10:{
                                   switch( d ){
                                       case 1 : return 2; break;
                                   }
                                    return false;
                                };break;
                        case 11:{
                                return false;
                                };break;
                        case 12:{
                                    switch( d ){
                                        case 25 : return 3;break;
                                    }
                                return false;
                                };break;
                        default:
                                return false;
                    }
                },
    isLurFesDays : function( day ){
                         var m = day.month;
                         var d = parseInt( day.day );
                    switch( m ){
                        case 1: {
                                    switch( d ){
                                        case 1 :{ return 5; }break;
                                    }
                                    return false;
                                };break;
                        case 2:{
                                return false;
                               };break;
                        case 3:{
                                return false;
                               };break;
                        case 4:{
                                return false;
                               };break;
                        case 5:{
                                   switch( d ){
                                       case 5:{return 6;} break;
                                   }
                                return false;
                               };break;
                        case 6:{
                                return false;
                               };break;
                        case 7:{
                                return false;
                               };break;
                        case 8:{
                                   switch( d ){
                                       case 15 : return 7 ; break;
                                   }
                                return false;
                               };break;
                        case 9:{
                                return false;
                               };break;
                        case 10:{
                                    return false;
                                };break;
                        case 11:{
                                return false;
                                };break;
                        case 12:{
                                return false;
                                };break;
                        default:
                                return false;

                    }
                },
    collectSpecialDays : function( days, daye ){
                             var res = [];
                             var flag = days;
                             var isLunar = 0;
                             var ty = (""+ flag).substr( 0, 4 );
                             var tm = (""+ flag).substr( 4, 2 );
                             tm = parseInt( tm );
                             var td = (""+ flag).substr( 6, 2 );
                             flag = new Date( ty, tm-1, td );
                             ty = (""+ daye).substr( 0, 4 );
                             tm = (""+ daye).substr( 4, 2 );
                             tm = parseInt( tm );
                             td = (""+ daye).substr( 6, 2 );
                             daye = new Date( ty, tm-1, td );
                             var allFes = '';
                             for( var i = 0; flag <= daye; i++ ){
                                 var ret = this.isFesDays( flag );
                                 if( ret && allFes.indexOf( ""+ ret ) < 0 )
                                 {
                                    var pq = this.formatDate( flag );
                                    res.push( pq );
                                    allFes += "" + ret;
                                    isLunar = 1;
                                 }
                                 else{
                                    var q = new Lunar( flag );
                                    var ret = this.isLurFesDays( q );
                                    if(  ret && allFes.indexOf( ""+ ret ) < 0 )
                                    {
                                       var pq = this.formatDate( flag );
                                        allFes += "" + ret;
                                        res.push( pq );
                                    }
                                 }
                                 flag = this.dateAdd( flag, "d", 1 );

                             }
                             return res;
                        },
    /*
     *  统计可以使用的月份
     *  如果不是2.28/2.29/N.30/N.31日则常规处理
     *  如果是
     *选择每月一次，则该页出现日期的选择（01-31），由用户选择在每月的某一个日期发出，如果用户选择的日期大于某个月所包含的最大天数，则在该月的最后一天发出。
     * */
    collectMonths : function( tmpDate ){
                        if( tmpDate == null || tmpDate == undefined ){
                            return null;
                        }
                        var ty = tmpDate.getFullYear();
                        var tm = tmpDate.getMonth();
                        var td = tmpDate.getDate();
                        this.days = [];
                        var flag = 0;
                        for( var q = 1; q < 7; q ++ )
                        {
                            var month_index = ( tm + q ) % 12;
                            var year_index = ( ( tm + q ) / 12 ) + ty;
                            var max_days = this.monthDays( year_index, month_index+ 1 );
                            //日期比较小，那么取最后一天
                            if( max_days <= td )
                            {
                                tmpDate = new Date( year_index, month_index, max_days );
                                var pq = this.formatDate( tmpDate );
                                this.days.push( pq );
                                //日期修正量
                                flag =  td - max_days;
                            }
                            else
                            {
                                 tmpData = this.dateAdd( tmpDate, 'm', 1 );
                                 //如果有日期上的增益，那么就应当适当的调整，用在二月份比较明显
                                 if( flag > 0 )
                                 {
                                     tmpDate = this.dateAdd( tmpDate, 'd', flag );
                                     flag = 0;
                                 }
                                 var pq = this.formatDate( tmpDate );
                                 this.days.push( pq );
                            }
                        }
                        return this.days;
                    },
    collectDays: function( tmpDate ){
                     if( tmpDate == null || tmpDate == undefined ){
                        return null;
                     }
                     this.days = [];
                     var flag = 0;
                     var halfyear = 365 * 24 * 3600 * 1000 / 2;
                     var start = new Date();
                     var m = start.getTime();
                     var t = 0;
                     var index = 0;
                     for( var q = 0; t - m <= halfyear; q++ )
                     {
                         tmpData = this.dateAdd( tmpDate, 'w', 1 );
                         t = tmpDate.getTime();
                         var pq = this.formatDate( tmpDate );
                         this.days.push( pq );
                    }
                     return this.days;
             },
   
    _debugformatDate : function( d ){
                     return d.getFullYear() + "年" + ( d.getMonth() + 1) + "月" + d.getDate() + "日";
                 },

    formatDate : function( d ){
                     return this.formatDateInt( d.getFullYear() ) + this.formatDateInt( ( d.getMonth() + 1) ) + this.formatDateInt( d.getDate() );
                 },
    formatDateInt: function( q ){
                       return parseInt( q ) > 9 ? ""+q : "0"+q ;
                   }
};
var custom_mail = new customMail();
